package cn.maidouya.org.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.org.domain.Department;
import cn.maidouya.org.query.DepartmentQuery;
import cn.maidouya.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     *   批量删除
     */
    @PatchMapping
    @ApiOperation(value = "批量删除部门",notes = "批量删除部门")
    public AjaxResult batchDel(@RequestBody Long[] ids) {
        try {
        departmentService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找部门",notes = "根据ID查找部门")
    public Department loadById(@PathVariable Long id) {
        Department one = departmentService.loadById(id);
        return one;
    }
    @GetMapping
    @ApiOperation(value = "查找全部部门",notes = "查找所有部门信息")
    public List<Department> loadAll() {
        List<Department> list = departmentService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找部门分页数据",notes = "分页查找所有部门信息")
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request) {
        System.err.println("token缓存： " +request.getHeader("token"));
        PageList<Department> pageList = departmentService.queryData(departmentQuery);
        return pageList;
    }

    @PutMapping
    @ApiOperation(value = "添加和修改部门",notes = "添加和修改部门")
    public AjaxResult saveAndUpdate(@RequestBody Department department) {
                try {
                    if (department.getId() == null){
                        departmentService.save(department);
                    } else {
                        departmentService.update(department);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败"+e.getMessage());
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "根据id删除部门")
    public AjaxResult delete(@PathVariable Long id) {
        try {
          departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "上级部门查找",notes = "上级部门查找")
    public  List<Department>  getDepttree() {
        List<Department> list = departmentService.getDepttree();
        return list;
    }
}